<?php

// +----------------------------------------------------------------------
// | 版权所有 2017-2027 云南环拓科技有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.yn123.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// | 注意：  这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用,
// |        不允许对程序代码以任何形式任何目的的再发布。
// +----------------------------------------------------------------------
// | Author: feraner <feraner@163.com> 
// | Date: 2019/12/23
// +----------------------------------------------------------------------

namespace app\index\controller\api;

use app\common\constants\ApiConstant;
use app\common\services\MarketPublishService;
use app\common\services\MarketService;
use think\App;
use think\Db;

/**
 * 个人中心 二手商品接口集
 * Class UserMarket
 * @package app\index\controller\api
 * @author feraner<feraner@163.com>
 * @date 2019/12/23
 */
class UserMarket extends ApiBase
{

    public function __construct(App $app = null)
    {
        parent::__construct($app);
    }


    /**
     * 我发布的二手商品
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function myPublishGoods()
    {
        $type = $this->request->param('type', 0);
        $page = $this->request->param('page', 1);
        $pageSize = $this->request->param('pageSize', 8);
        $where = [
            'page'              => $page,
            'pageSize'          => $pageSize,
            'publishUserId'     => $this->userId,
            'status'            => $type
        ];
        $goodsList = (new MarketService())->goodsList(
            $where,
            $page,
            $pageSize
        );

        $this->response($goodsList);
    }


    /**
     * 删除二手商品
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function delGoods()
    {
        $goodsId = $this->request->post('id', 0);
        $goodsInfo = Db::name('market')
            ->where('id', $goodsId)
            ->where('publish_id', $this->userId)
            ->find() ?: [];
        if ($goodsInfo) {
            try {
                $result = (new MarketService())->deleteGoods($goodsInfo, $this->userId);
                if ($result) {
                    $this->response([
                        'goodsId'     => $goodsId
                    ]);
                } else {
                    $this->response(
                        [],
                        ApiConstant::API_ERROR,
                        ApiConstant::API_ERROR_STR
                    );
                }
            } catch (\Exception $e) {
                $this->response(
                    [],
                    ApiConstant::API_ERROR,
                    $e->getMessage()
                );
            }
        }
        $this->response(
            [],
            ApiConstant::API_PARAMETER_ERROR,
            ApiConstant::API_PARAMETER_ERROR_STR
        );
    }


    /**
     * 审核用户发布的二手商品
     */
    public function auditGoods()
    {
        $goodsId = $this->request->post('goodsId', 0);
        $pass = $this->request->post('pass', 0);
        $message = $this->request->post('message', '内容敏感');
        if ($goodsId && $pass) {
            try {
                $publishMarketService = new MarketPublishService();
                $status = 1;
                if ($pass == 1) {
                    //审核同意
                    $result = $publishMarketService->approvedGoods($goodsId, $this->userId);
                    $status = 3;
                } else {
                    //拒绝
                    $result = $publishMarketService->rejectGoods($goodsId, $this->userId, $message);
                    $status = 4;
                }
                if ($result) {
                    $this->response([
                        'pass'    => $pass,
                        'status'  => $status
                    ]);
                }
            } catch (\Exception $e) {
                $this->response(
                    [],
                    ApiConstant::MARKET_GOODS_AUDIT_ERROR,
                    ApiConstant::MARKET_GOODS_AUDIT_ERROR_STR . ':' . $e->getMessage()
                );
            }
            $this->response(
                [],
                ApiConstant::MARKET_GOODS_AUDIT_ERROR,
                ApiConstant::MARKET_GOODS_AUDIT_ERROR_STR
            );
        }
        $this->response(
            [],
            ApiConstant::API_PARAMETER_ERROR,
            ApiConstant::API_PARAMETER_ERROR_STR
        );


    }






    /**
     * 收藏的二手商品列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function collectGoodsList()
    {
        $page = $this->request->param('page', 1);
        $pageSize = $this->request->param('pageSize', 8);

        $goodsCollectList = Db::name('user_collect')
            ->where('user_id', $this->userId)
            ->where('type', 2)
            ->order('id DESC')
            ->page($page, $pageSize)
            ->select() ?: [];

        $result = [];
        if ($goodsCollectList) {
            $result = (new MarketService())->goodsList(
                [
                    'goodsIdArr'    => array_column($goodsCollectList, 'collect_id')
                ],
                1,
                8
            );
        }

        $this->response($result);

    }






}
